/*!
 * \file t___http_req_header_detect.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../../src/module/http/config.h"
#include <http/comm/http_comm.h>
#include <http/http1/http_analysis.h>
#include <http/http_core.h>
#include <frct_comm.h>
#include "frreport_comm.h"
#include "../../../comm/apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern char * __http_req_header_detect(char *p_tcp_body, unsigned int ui_tcp_body_len, char **pp_uri
        , int *p_i_uri_len, int *p_i_option, Frreport_body_data *p_st_body_data);

static void __case1(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    char *p_in_tmp = NULL;
    char *p_uri = NULL;
    int i_tcp_body_len = 0;
    int i_option = 0;
    int i_uri_len = 0;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;

    fp_file = fopen("t_http1/t_http_analysis/req_header_detect1.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    p_in_tmp = __http_req_header_detect(p_tcp_body, i_tcp_body_len, &p_uri, &i_uri_len, &i_option, p_st_body_data);

    CU_ASSERT_PTR_NOT_NULL(p_in_tmp);
    CU_ASSERT_EQUAL(i_uri_len,25);
    CU_ASSERT_EQUAL(i_option,2);
    CU_ASSERT_STRING_EQUAL(p_uri,"/include/httptest.inc.php");

    frfree(p_tcp_body);
    frfree(p_uri);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void __case2(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    char *p_in_tmp = NULL;
    char *p_uri = NULL;
    int i_tcp_body_len = 0;
    int i_option = 0;
    int i_uri_len = 0;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;

    fp_file = fopen("t_http1/t_http_analysis/req_header_detect2.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    p_in_tmp = __http_req_header_detect(p_tcp_body, i_tcp_body_len, &p_uri, &i_uri_len, &i_option, p_st_body_data);

    CU_ASSERT_PTR_NOT_NULL(p_in_tmp);
    CU_ASSERT_EQUAL(i_uri_len,1);
    CU_ASSERT_EQUAL(i_option,1);
    CU_ASSERT_STRING_EQUAL(p_uri,"/");

    frfree(p_tcp_body);
    frfree(p_uri);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

static void __case3(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    char *p_in_tmp = NULL;
    char *p_uri = NULL;
    int i_tcp_body_len = 0;
    int i_option = 0;
    int i_uri_len = 0;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;

    fp_file = fopen("t_http1/t_http_analysis/req_header_detect3.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    p_in_tmp = __http_req_header_detect(p_tcp_body, i_tcp_body_len, &p_uri, &i_uri_len, &i_option, p_st_body_data);

    CU_ASSERT_PTR_NOT_NULL(p_in_tmp);
    CU_ASSERT_EQUAL(i_uri_len,47);
    CU_ASSERT_EQUAL(i_option,1);
    CU_ASSERT_STRING_EQUAL(p_uri,"/res/static/thirdparty/connect.jpg?t=1426122191");

    frfree(p_tcp_body);
    frfree(p_uri);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}


static void __case4(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    char *p_in_tmp = NULL;
    char *p_uri = NULL;
    int i_tcp_body_len = 0;
    int i_option = 0;
    int i_uri_len = 0;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;

    fp_file = fopen("t_http1/t_http_analysis/req_header_detect4.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    p_in_tmp = __http_req_header_detect(p_tcp_body, i_tcp_body_len, &p_uri, &i_uri_len, &i_option, p_st_body_data);

    CU_ASSERT_PTR_NULL(p_in_tmp);

    frfree(p_tcp_body);
    frfree(p_uri);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);

    return;
}

int add_suite___http_req_header_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __http_req_header_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
